# 重构后的主程序
# TODO(EzioTAuditore)   实在懒得干了，还有个鼠标图标变化的功能不写了
# TODO(EzioTAuditore)   有个BUG，双击目标不但会失败，还会加双倍分数和生命数,直接写在玩法里算了

# 导入区域
import pygame,os,sys,random,webbrowser

# 初始化区域
pygame.init()
pygame.mixer.init()
os.chdir(sys.path[0])
myfont = pygame.font.Font(None, 70)     # 字体初始化

# 变量定义区域
## 图片变量
### 开始界面
image_screen_GameTittle = pygame.image.load('./image/screen_GameTittle.png')#标题
image_screen_bgImage = pygame.image.load('./image/screen_bgImage.png')
image_screen_StartGame = pygame.image.load('./image/screen_StartGame.png')
image_screen_QuitGame = pygame.image.load('./image/screen_QuitGame.png')
image_credits = pygame.image.load('./image/credits.png')
### 游戏界面
image_gameScreen_GameImage = pygame.image.load('./image/gameScreen_GameImage.png')
image_gameScreen_StatusImage = pygame.image.load('./image/gameScreen_StatusImage.png')
image_gameScreen_PauseGame = pygame.image.load('./image/gameScreen_PauseGame.png')
image_gameScreen_QuitGame = pygame.image.load('./image/gameScreen_QuitGame.png')
### 游戏互动
image_target = pygame.image.load('./image/target.png')
image_arms = pygame.image.load('./image/arms.png')
### 结束界面
image_endScreen_bgImage = pygame.image.load('./image/endScreen_bgImage.png')
image_endScreen_QuitGame = pygame.image.load('./image/endScreen_QuitGame.png')
image_endScreen_MainMenu = pygame.image.load('./image/endScreen_MainMenu.png')
## 音乐变量
music_cickDown = pygame.mixer.Sound('./music/cickDown.mp3')
music_cickUp = pygame.mixer.Sound('./music/cickUp.mp3')
music_hit = pygame.mixer.Sound('./music/hit.mp3')
music_screenbgm = pygame.mixer.Sound('./music/screenbgm.mp3')
music_gamebgm = pygame.mixer.Sound('./music/gamebgm.mp3')
music_endbgm = pygame.mixer.Sound('./music/endbgm.mp3')
## 坐标抽出
xy_screen_bgImage = (0,0)
xy_screen_GameTittle = (114,40)
xy_screen_StartGame = (169,217)
xy_screen_QuitGame = (205,284)
xy_credits = (670,520)
xy_gameScreen_GameImage = (0,0)
xy_gameScreen_StatusImage = (600,0)
xy_gameScreen_PauseGame = (640,270)
xy_gameScreen_QuitGame = (640,370)
xy_scroe = (710,20)
xy_Lv = (710,71)
xy_target_live = (710,121)
xy_endScreen_bgImage = (0,0)
xy_endScreen_Scroe = (321,259)
xy_endScreen_QuitGame = (480,340)
xy_endScreen_MainMenu = (169,346)
## 鼠标点击声
def cick():
    music_cickDown.play(0)
    music_cickUp.play(0)
    pygame.time.delay(20)
## 颜色变量定义区
color_white = [255,255,255]
color_black = [0,0,0]
## 功能变量区
clock = pygame.time.Clock()
screen_size = [800,600] # 窗口尺寸
target_kill = 0     # 击杀数
score = 0   # 分数
Lv = 0  # 等级
speed = 1000    # 速度
target_live = 10 # 存活数，判断游戏失败
i = 0   # 配合存活数计算
running = True

# 格式化游戏窗口
gameScreen = pygame.display.set_mode(screen_size)
area_gameScreen_PauseGame = pygame.Rect(xy_gameScreen_PauseGame[0],xy_gameScreen_PauseGame[1],120,60)
area_gameScreen_QuitGame = pygame.Rect(xy_gameScreen_QuitGame[0],xy_gameScreen_QuitGame[1],120,60)

# 格式化游戏结束窗口
endScreen = pygame.display.set_mode(screen_size)
area_endScreen_QuitGame = pygame.Rect(xy_endScreen_QuitGame[0],xy_endScreen_QuitGame[1],200,60)
area_endScreen_MainMenu = pygame.Rect(xy_endScreen_MainMenu[0],xy_endScreen_MainMenu[1],124,38)

# 游戏结算界面
def end():
    # 初始化
    music_gamebgm.stop()
    music_endbgm.set_volume(0.7)
    music_endbgm.play(0)
    endScreen.blit(image_endScreen_bgImage,xy_endScreen_bgImage)
    font_endScreen_Scroe = myfont.render((str)(score),True,color_black)
    endScreen.blit(font_endScreen_Scroe,xy_endScreen_Scroe)
    endScreen.blit(image_endScreen_QuitGame,xy_endScreen_QuitGame)
    endScreen.blit(image_endScreen_MainMenu,xy_endScreen_MainMenu)
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.MOUSEBUTTONUP and area_endScreen_QuitGame.collidepoint(event.pos)):
                cick()
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONUP and area_endScreen_MainMenu.collidepoint(event.pos):
                # 初始化计分板
                global target_kill,Lv,speed,target_live,i
                target_kill =  Lv = i = 0
                speed = 1000
                target_live = 10
                main()  

# 更新状态栏
def updateStatus():
    gameScreen.blit(image_gameScreen_GameImage,xy_gameScreen_GameImage)
    gameScreen.blit(image_gameScreen_StatusImage,xy_gameScreen_StatusImage)
    gameScreen.blit(image_gameScreen_PauseGame,xy_gameScreen_PauseGame)
    gameScreen.blit(image_gameScreen_QuitGame,xy_gameScreen_QuitGame)
    pygame.display.flip()

# 分数功能
def scoreboard():
    global score,Lv,speed,target_live
    score = target_kill*10
    Lv = target_kill//10
    speed = 1000 - target_kill*10
    target_live = i - target_kill
    # 游戏结束判断
    if target_live >10:
        end()
    # 格式化分数字符对象
    font_Lv = myfont.render((str)(Lv),True,color_black)
    font_score = myfont.render((str)(score),True,color_black)
    font_target_live = myfont.render((str)(10-target_live),True,color_black)
    # 输出部分：分数，等级，剩余次数
    gameScreen.blit(font_score,xy_scroe)
    gameScreen.blit(font_Lv,xy_Lv)
    gameScreen.blit(font_target_live,xy_target_live)
    pygame.display.update()

# 暂停功能
def pause():
    #  使用running变量来控制暂停功能是否开启
    global running
    running = True
    while running:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN and area_gameScreen_QuitGame.collidepoint(event.pos):
                cick()
                end()   
            elif event.type == pygame.MOUSEBUTTONDOWN and area_gameScreen_PauseGame.collidepoint(event.pos):
                cick()
                running = False
                pygame.time.delay(1000)

# game()函数，用来实现游戏内部的功能
def game():
    while True:
        clock.tick(60)
        global i
        for i in range(0,100):
            # 输出苍蝇
            ## 苍蝇坐标生成,苍蝇区域生成
            # 刷新出苍蝇前清空屏幕
            # gameScreen.blit(image_gameScreen_GameImage,[0,0])
            updateStatus()
            scoreboard()
            (target_x,target_y) = (random.randint(0,550),random.randint(0,550))
            gameScreen.blit(image_target,(target_x,target_y))
            area_target = pygame.Rect(target_x,target_y,50,50)
            pygame.display.update()
            pygame.time.delay(speed)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.MOUSEBUTTONDOWN and area_target.collidepoint(event.pos):
                    music_hit.play(0)   # 打击音效
                    # 击中目标
                    gameScreen.blit(image_gameScreen_GameImage,[0,0])
                    pygame.display.flip()
                    # 记录击杀数
                    global target_kill
                    target_kill += 1
                    pygame.time.delay(20)
                # 监听退出
                elif event.type == pygame.MOUSEBUTTONDOWN and area_gameScreen_QuitGame.collidepoint(event.pos):
                    cick()
                    end()
                # 监听暂停
                elif event.type == pygame.MOUSEBUTTONDOWN and area_gameScreen_PauseGame.collidepoint(event.pos):
                    cick()
                    pause()

def main():
    # 写一个循环，保持界面不会退出。
    # 格式化开始界面
    screen = pygame.display.set_mode(screen_size)
    pygame.display.set_caption('打苍蝇小游戏')
    screen.blit(image_screen_bgImage,xy_screen_bgImage)
    screen.blit(image_screen_GameTittle,xy_screen_GameTittle)
    screen.blit(image_screen_StartGame,xy_screen_StartGame)
    screen.blit(image_screen_QuitGame,xy_screen_QuitGame)
    screen.blit(image_credits,xy_credits)
    music_screenbgm.play(-1)
    ## 开始界面按钮区域
    area_screen_StartGame = pygame.Rect(xy_screen_StartGame[0],xy_screen_StartGame[1],190,45)
    area_screen_QuitGame = pygame.Rect(xy_screen_QuitGame[0],xy_screen_QuitGame[1],125,26)
    area_credits = pygame.Rect(xy_credits[0],xy_credits[1],100,50)
    pygame.display.flip()
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.MOUSEBUTTONDOWN and area_screen_QuitGame.collidepoint(event.pos)):
                cick()
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN and area_screen_StartGame.collidepoint(event.pos):
                cick()
                music_screenbgm.stop()
                music_gamebgm.play(-1)
                # 使用game()函数
                game()
            elif event.type == pygame.MOUSEBUTTONDOWN and area_credits.collidepoint(event.pos):
                # 开发名单直接引导到gitee的项目界面
                webbrowser.open('https://gitee.com/eziotauditore/pygame', new=0, autoraise=True)
                

if __name__ == "__main__":
    main()